package servlet.listener;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import org.apache.tomcat.dbcp.dbcp.BasicDataSource;

public class DataSourcelistener implements ServletContextListener {

    public DataSourcelistener() {
    }

    public void contextDestroyed(ServletContextEvent arg0) {
    }

    public void contextInitialized(ServletContextEvent arg0) {
    	//BasicDataSource 생성 - application scope의 attribute로 bindig
    	//drivetclassname,url,user,password + connection pool 설정
    	ServletContext ctx = arg0.getServletContext();
//    	초기 파라미터 조회
    	String drivername = ctx.getInitParameter("driverName");
    	String url = ctx.getInitParameter("db url");
    	String userName = ctx.getInitParameter("db user");
    	String password = ctx.getInitParameter("db password");
    	System.out.println(drivername+" "+url+" "+userName+" "+password); 
    	BasicDataSource ds = new BasicDataSource();
    	ds.setDriverClassName(drivername);
    	ds.setUrl(url);
    	ds.setUsername(userName);
    	ds.setPassword(password);
    	
    	ctx.setAttribute("dataSource", ds);
    }
	
}
